// 定义一些公共的属性和方法

// 父级关系的同级数据转换成树形数据
function toTreeData(data, treeAttr) {
  // // 属性配置设置
  // let treeAttr = {
  //   id: 'id',
  //   parendId: 'pid',
  //   name: 'name',
  //   rootId: 0
  // };
  let tree = [];
  let resData = data;
  for (let i = 0; i < resData.length; i++) {
    if (resData[i][treeAttr.parendId] == treeAttr.rootId) {


      resData[i].id = resData[i][treeAttr.id];
      resData[i].name = resData[i][treeAttr.name];
      resData[i].children = [];
      // let obj = {
      //   id: resData[i][treeAttr.id],
      //   name: resData[i][treeAttr.name],
      //   children: []
      // };
      // tree.push(obj);
      tree.push(resData[i]);
      resData.splice(i, 1);
      i--;
    }
  }
  var run = function (treeArrs) {
    if (resData.length > 0) {
      for (let i = 0; i < treeArrs.length; i++) {
        for (let j = 0; j < resData.length; j++) {
          if (treeArrs[i].id === resData[j][treeAttr.parendId]) {

            resData[j].id = resData[j][treeAttr.id];
            resData[j].name = resData[j][treeAttr.name];
            resData[j].children = [];
            // let obj = {
            //   id: resData[j][treeAttr.id],
            //   name: resData[j][treeAttr.name],
            //   children: []
            // };
            // treeArrs[i].children.push(obj);
            treeArrs[i].children.push(resData[j]);
            resData.splice(j, 1);
            j--;
          }
        }
        run(treeArrs[i].children);
      }
    }
  };
  run(tree);
  return tree;
}


// tree 数据转换
/**
 * tree 数据转换
 * @param  {Array} tree 待转换的 tree
 * @param  {Object} map  键值对映射
 * @return {Array}      转换后的 tree
 * selectData 返回选中的数据
 */
function convertTree(tree, map, selectData) {
  // var map = {
  //   value: 'id',
  //   title: 'name',
  //   children: 'children'
  // }
  const result = []
  // 遍历 tree
  tree.forEach((item) => {
    // 读取 map 的键值映射

    item["value"] = item[map.value]
    item["title"] = item[map.title]
    item["children"] = item[map.children]

    if (!!selectData && selectData.length > 0) {
      for (var io = 0; io < selectData.length; io++) {
        if (item[map.value] == selectData[io]) {
          item["checked"] = true;
        }
      }
    }

    // const value = item[map.value]
    // const title = item[map.title]
    let children = item[map.children]

    // 如果有子节点，递归
    if (children) {
      children = convertTree(children, map, selectData)
    }
    result.push(item)
  })
  return result
}


//获得当前时间 时分秒 时间格式化
function timeFormatting(times, format) {
  var time = new Date(times);
  var o = {
    "M+": time.getMonth() + 1,
    "d+": time.getDate(),
    "h+": time.getHours(),
    "m+": time.getMinutes(),
    "s+": time.getSeconds(),
    "q+": Math.floor((time.getMonth() + 3) / 3),
    "S": time.getMilliseconds()
  }
  if (/(y+)/.test(format)) {
    format = format.replace(RegExp.$1, (time.getFullYear() + "").substr(4 - RegExp.$1.length));
  }
  for (var k in o) {
    if (new RegExp("(" + k + ")").test(format)) {
      format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
    }
  }
  return format;
}

function wzSeeFile(type) {//文档和视频查看
  //type 文件类型 传一个连接 这里解析
  //data 文件数据  可能是数组 也可能是单挑数据
  var typeW;
  var name = type.substr((type.lastIndexOf(".") + 1))
  if (name == "MP4" || name == "OGG" || name == "WEBM" || name == "mp4" || name == "ogg" || name == "webm" || name == "m3u8" || name == "M3U8") {//视频
    typeW = 1
  } else if (name == "DOC" || name == "XLS" || name == "PPT" || name == "RTF" || name == "DOCX" || name == "doc" || name == "xls" || name == "ppt" || name == "rtf" || name == "docx") {//word
    typeW = 2
  } else if (name == "BMP" || name == "JPG" || name == "JPEG" || name == "PNG" || name == "GIF" || name == "bmp" || name == "jpg" || name == "jpeg" || name == "png" || name == "gif") {//图片
    typeW = 3
  } else if (name == "PDF" || name == "pdf") {//图片
    typeW = 4
  } else if (name == "html" || name == "HTML") {
    typeW = 5;
  }
  return typeW;
}

//图片404处理
function imgDispose(event) {
  event.target.src = "../../static/img/zwtpxd.png";
}


//路由 和权限对比  获取首页  开始
/*
* 1.循环路由
* 2.有子级  循环子级
* 3.没子级 放过
* 4.子级如果有子级  再次循环
* 5.子级 如果有权限标识  去循环权限
* 6.有 return false; 获取路由跳转
* 7.如果没有  循环下一个子级
* 8.如果子级 没有权限表示  获取路由  return false;  跳转
* */
function getHomePage() {
  var routers = JSON.parse(sessionStorage.getItem("loginData")).menu.SAFETY;//路由
  return loopRouting(routers);
}

//循环路由
function loopRouting(data) {
  for (var i = 0; i < data.length; i++) {//循环路由数据
    if (data[i].children.length > 0) {//有子级 递归再次循环
      return loopRouting(data[i].children)
    } else {//没有子级
      return data[i].menuUrl;
    }
  }
}


//路由 和权限对比  获取首页  结束


// that.public.方法名(参数)

// 暴露出这些属性和方法
export default {
  toTreeData,
  convertTree,
  timeFormatting,
  wzSeeFile,
  imgDispose,
  getHomePage,

}


